package com.cindata

import com.cindata.dictionary.*
import org.springframework.security.access.annotation.Secured

import java.text.DecimalFormat

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Secured(['isAuthenticated()'])
@Transactional
class RiskWarningLevelController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) 
    {
        params.max = Math.min(max ?: 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        max = params.max
        def name = params['name']
        def offset = params.offset
        def sql = "  from RiskWarningLevel m where 1=1 "
        if (name)
        {
            sql += " and name like '%${name}%' "
        }
        def sql1 = " select count(*) " + sql
        sql += "order by m.modifiedDate asc "
        def riskWarningLevelList = RiskWarningLevel.executeQuery(sql, [max: max, offset: offset]).asList()
        def count = RiskWarningLevel.executeQuery(sql1)
        respond riskWarningLevelList, model: [riskWarningLevelList: riskWarningLevelList, riskWarningLevelCount: count[0]]
    }

    def show(RiskWarningLevel riskWarningLevel) {
        respond riskWarningLevel
    }

    def create() 
    {
        params['targetUri'] = request.getHeader("referer")
        respond new RiskWarningLevel(params)
    }

    @Transactional
    def save(RiskWarningLevel riskWarningLevel) {
        if (riskWarningLevel == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningLevel.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningLevel.errors, view:'create'
            return
        }

        riskWarningLevel.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'riskWarningLevel.label', default: 'RiskWarningLevel'), riskWarningLevel.id])
                redirect url: params['targetUri']
            }
            '*' { respond riskWarningLevel, [status: CREATED] }
        }
    }

    def edit(RiskWarningLevel riskWarningLevel) 
    {
        params['targetUri'] = request.getHeader("referer")
        respond riskWarningLevel
    }

    @Transactional
    def update(RiskWarningLevel riskWarningLevel) 
    {
        if (riskWarningLevel == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningLevel.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningLevel.errors, view:'edit'
            return
        }

        riskWarningLevel.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'riskWarningLevel.label', default: 'RiskWarningLevel'), riskWarningLevel.id])
                redirect url: params['targetUri']
            }
            '*'{ respond riskWarningLevel, [status: OK] }
        }
    }

    @Transactional
    def delete(RiskWarningLevel riskWarningLevel) {

        if (riskWarningLevel == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        riskWarningLevel.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'riskWarningLevel.label', default: 'RiskWarningLevel'), riskWarningLevel.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'riskWarningLevel.label', default: 'RiskWarningLevel'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
